﻿using System;
using NLog;
using NUnit.Framework;
using ScrappyDB.Attributes;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests.Core
{
    
    
    /// <summary>
    ///This is a test class for DBTest and is intended
    ///to contain all DBTest Unit Tests
    ///</summary>
    [TestFixture]
    public class DBTest
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [TestFixtureSetUp]
        public  void Init()
        {
            Cleanup();
        }
        
        [TearDown]
        public void MyTestCleanup()
        {
            Cleanup();
        }

        private static void Cleanup()
        {
            try
            {
                var db = new Db();
                var a = db.Query<TestItem>(null, true);

                foreach (var i in a)
                {
                    db.Delete(i);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
        #endregion

        [DomainName("ScrappyDB_UnitTests"), EntityName("TestItem")]
        class TestItem 
        {
            public Guid Id { get; set; }
            public string TestString { get; set; }
        }

        [Test]
        public void QueryTestWithStringFormat()
        {
            var db = new Db();

            var o1 = new TestItem()
            {
                TestString = "A",
                Id = Guid.Empty
            };
            db.SaveChanges(o1);

            var o2 = new TestItem()
            {
                TestString = "B",
                Id = Guid.NewGuid()
            };
            db.SaveChanges(o2);


            bool consistentRead = true;

            string query1 = " TestString = '{0}' ";
            var actual1 = db.Query<TestItem>(query1, consistentRead, "A");
            Assert.AreEqual(1, actual1.Count);

            string query2 = " TestString = '{0}' or ItemName() = '{1}' ";
            var actual2 = db.Query<TestItem>(query2, consistentRead, "B", Guid.Empty);
            Assert.AreEqual(2, actual2.Count);


        }



    }
}
